package com.itls.recommendedgames.control;


import com.itls.recommendedgames.entity.Game;
import com.itls.recommendedgames.entity.User;
import com.itls.recommendedgames.mapper.GameMapper;
import com.itls.recommendedgames.service.RecommendationService;
import com.itls.recommendedgames.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;

@RestController
@RequestMapping("/recommendations")
@CrossOrigin
@Tag(name="推荐系统")
public class RecommendationController {

    @Autowired
    private RecommendationService recommendationService;

    @Autowired
    private UserService userService;

    @Autowired
    private GameMapper gameMapper;


    /**
     * 根据用户ID获取 User 对象
     */
    private User getUserById(Integer userId) {
        // 创建一个基本的User对象，只需要ID即可
        User user = new User();
        user.setId(userId.intValue());
        return user;
    }


    /**
     * 获取个性化推荐游戏
     */
    @GetMapping("/personalized")
    @Operation(summary = "获取个性化推荐游戏")
    public ResponseEntity<List<Game>> getPersonalizedRecommendations(
            @RequestParam Integer userId,
            @RequestParam(defaultValue = "10") int limit) {
        try {
            User user = getUserById(userId);
            List<Game> recommendations = recommendationService.getPersonalizedRecommendations(user, limit);
            return ResponseEntity.ok(recommendations);
        } catch (Exception e) {
            System.err.println("个性化推荐异常: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 获取热门游戏推荐
     */
    @GetMapping("/popular")
    @Operation(summary = "获取热门游戏推荐")
    public ResponseEntity<List<Game>> getPopularGames(
            @RequestParam Integer userId,
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(defaultValue = "false") boolean includeTrending) {
        try {
            List<Game> popularGames = recommendationService.getPopularGamesForUser(userId,limit, includeTrending);
            return ResponseEntity.ok(popularGames);
        } catch (Exception e) {
            System.err.println("获取热门游戏异常: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.internalServerError().build();
        }
    }
}